package com.qys.livesMall.shop.service;

import com.qys.livesMall.shop.constants.OrderStatus;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @menu ShopOrderStatusManager
 * @date 2023/09/22 9:59
 */
@Service
public class ShopOrderStatusManager {

    public Integer getMainOrderStatus(List<ShopOrderSku> skus){
        List<Integer> statuses= skus.stream().map(ShopOrderSku::getOrderStatus).collect(Collectors.toList());
        List<Integer> beforeStatuses= skus.stream().map(ShopOrderSku::getReturnBeforeStatus).collect(Collectors.toList());
        int orderNum=skus.size();
        //如果只有一个子单
        if (orderNum==1){
            return skus.get(0).getOrderStatus();
        }
        //多个子单
        Map<Integer,List<Integer>> statusMap=statuses.stream().collect(Collectors.groupingBy(o->o));
        Map<Integer,List<Integer>> beforeStatusesMap=beforeStatuses.stream().filter(o->o!=null).collect(Collectors.groupingBy(o->o));
        Integer bShipped=beforeStatusesMap.get(OrderStatus.SHIPPED)==null?0:beforeStatusesMap.get(OrderStatus.SHIPPED).size();
        Integer bReceived=beforeStatusesMap.get(OrderStatus.RECEIVED)==null?0:beforeStatusesMap.get(OrderStatus.RECEIVED).size();
        //存在退货/换货/退款
        Integer returnApplied=statusMap.get(OrderStatus.RETURN_APPLIED)==null?0:statusMap.get(OrderStatus.RETURN_APPLIED).size();
        Integer returnConfirmed=statusMap.get(OrderStatus.RETURN_CONFIRMED)==null?0:statusMap.get(OrderStatus.RETURN_CONFIRMED).size();
        Integer returned=statusMap.get(OrderStatus.RETURNED)==null?0:statusMap.get(OrderStatus.RETURNED).size();
//        Map<Integer,Map<Integer,Integer>> allStatusMap=new HashMap<>();
//        for(Integer key:statusMap.keySet()){
//            List<ShopOrderSku> orderSkus=statusMap.get(key);
//            Map<Integer,List<ShopOrderSku>> beforeMap= orderSkus.stream().collect(Collectors.groupingBy(ShopOrderSku::getReturnBeforeStatus));
//            allStatusMap.put(key,)
//        }
        //存在未发货
        Integer unShipped=statusMap.get(OrderStatus.UNSHIPPED)==null?0:statusMap.get(OrderStatus.UNSHIPPED).size();

        if (unShipped>0){
            if (unShipped== skus.size()){
                return OrderStatus.UNSHIPPED;
            }

            if (bReceived>0||bShipped>0){
                return OrderStatus.SHIPPED;
            }
            if (unShipped+returnConfirmed+returnConfirmed+returnConfirmed==skus.size()) {
                return OrderStatus.UNSHIPPED;
            }
        }
        //存在已发货
        List<Integer> shipped=statusMap.get(OrderStatus.SHIPPED);
        if (!CollectionUtils.isEmpty(shipped)){
            return OrderStatus.SHIPPED;
        }
        //存在已收货
       Integer received=statusMap.get(OrderStatus.RECEIVED)==null?0:statusMap.get(OrderStatus.RECEIVED).size();
        if (received>0){
            if (unShipped>0) {
                return OrderStatus.SHIPPED;
            }
            if (received+returnConfirmed+returnConfirmed+returnConfirmed==skus.size()){
                return OrderStatus.RECEIVED;
            }
        }

        //全部都在售后中
        if (returnApplied+returnConfirmed+returned==statuses.size()){
            if (returnApplied>0){
                return OrderStatus.RETURN_APPLIED;
            }
            if (returnConfirmed>0){
                return OrderStatus.RETURN_CONFIRMED;
            }
            return OrderStatus.RETURNED;
        }
        return statuses.get(0);
    }
}

